import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/bean/shopcart/ShopingCartBean.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/bean/subitorder/SubitOrderBean.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/bean/subitorder/SubitOrderEntity.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/Constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/ImageHelper.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/RouteHelper.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/ViewHelper.dart';

import 'SubitOrderVM.dart';

ValueNotifier<List<ShoppingCartLists>> selectCartGoodVN = ValueNotifier([]);

class SubitOrderWidget extends StatelessWidget {

  final SubitOrderVM subitVM = SubitOrderVM();

  @override
  Widget build(BuildContext context) {
    List<SubitOrderEntity>? list = getSubitOrderArguments(context);
    if(list==null) {
      showBottomToast("数据异常");
      Navigator.of(context).pop();
      return Text("");
    }
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      subitVM.getSubitOrderInfo(list);
    });
    return Scaffold(
      body: Container(
        color: Color(ConstantColors.mainBackgroundColor),
        child: Column(
          children: [
            //标题栏
            DufTitleBar(
              backColor: Colors.black,
              titleText: "确认订单",
              color: Colors.white,
            ),
            Expanded(
              child: OrderBody(subitVM),
            ),
            //底部栏
            ValueListenableBuilder(
              builder: (BuildContext context, List<ShoppingCartLists> value,
                  Widget? child) {
                return Container(
                  height: 50.h,
                  color: Colors.white,
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.fromLTRB(26.sp, 0, 26.sp, 0),
                  child: Row(
                    children: [
                      Expanded(
                          child: PriceInfoDealWith(
                              priceText: '',
                              symbolSize: 16.sp,
                              priceSize: 16.sp,
                              isAskPriceFlag: 1)),
                      //结算按钮
                      TextButton(
                        onPressed: () {
                          backAppMainAndTab(context,3);
                          toOrderWidget(context);
                        },
                        child: Text(
                          "提交订单",
                          style:
                              TextStyle(color: Colors.white, fontSize: 14.sp),
                        ),
                        style: ButtonStyle(
                          minimumSize:
                              MaterialStateProperty.all(Size(108.sp, 38.sp)),
                          maximumSize:
                              MaterialStateProperty.all(Size(128.sp, 38.sp)),
                          backgroundColor: MaterialStateProperty.all(
                              Color(ConstantColors.mainColor)),
                          shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(19.sp))),
                        ),
                      )
                    ],
                  ),
                );
              },
              valueListenable: selectCartGoodVN,
            )
          ],
        ),
      ),
    );
  }
}

class OrderBody extends StatefulWidget {

   SubitOrderVM subitVM ;

   OrderBody(this.subitVM);

  @override
  _OrderBodyState createState() => _OrderBodyState();
}

class _OrderBodyState extends State<OrderBody> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (BuildContext context) {
         return widget.subitVM;
      },
      child: Container(
        padding: EdgeInsets.fromLTRB(12.sp, 0, 12.sp, 0),
           child: SingleChildScrollView(
              child: Column(
                children: [
                  AddressWidge(widget.subitVM),
                  StoreListWidget(widget.subitVM),
                  PaymentFrom(),
                  Payway(),
                  Invoice(),
                  Padding(padding: EdgeInsets.fromLTRB(0, 20.sp, 0, 0))
                ],
              ),
           ),
      ),
    );
  }
}

class StoreListWidget extends StatefulWidget {

  SubitOrderVM vm;

  ScrollController _controller=ScrollController();

  StoreListWidget(this.vm);

  @override
  _StoreItemWidgetState createState() => _StoreItemWidgetState();
}

class _StoreItemWidgetState extends State<StoreListWidget> {

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Selector(
      builder: (BuildContext context,SubitOrderBean? value, Widget? child) {
         return ListView.builder(
           controller: widget._controller,
           padding: EdgeInsets.zero,
           shrinkWrap: true,
           itemCount: value?.confirmOrderItems?.length??0,
           itemBuilder: (BuildContext context, int index) {
             ConfirmOrderItems? bean= value?.confirmOrderItems?[index];
             return StoreItemWidget(widget._controller,bean,widget.vm);
           },);
      },
      selector: (BuildContext context,SubitOrderVM vm ) {
         return vm.subitOrderBean;
      },
    );
  }
}

//地址栏
class AddressWidge extends StatelessWidget {

  SubitOrderVM vm;
  
  AddressWidge(this.vm);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(0, 14.sp, 0, 0),
      padding: EdgeInsets.all(14.sp),
      decoration: ShapeDecoration(
          color: Colors.white,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10.sp))),
      child: Selector(
        builder: (BuildContext context,MemberReceiveAddress? value, Widget? child) {

          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Expanded(
                      child: Text(
                        "选择发货方式",
                        style: TextStyle(fontSize: 12.sp, color: Colors.black),
                      )),
                  DufCheckbox(
                    value: false,
                    radius: 7.sp,
                    onChanged: (value) {},
                  ),
                  Text(
                    "物流寄送",
                    style: TextStyle(fontSize: 12.sp, color: Colors.black),
                  ),
                  Padding(padding: EdgeInsets.fromLTRB(0, 0, 20.sp, 0)),
                  DufCheckbox(
                    value: false,
                    radius: 7.sp,
                    onChanged: (value) {},
                  ),
                  Text(
                    "客户自取",
                    style: TextStyle(fontSize: 12.sp, color: Colors.black),
                  ),
                ],
              ),
              Divider(
                height: 25.sp,
                color: Colors.transparent,
              ),
              AddressItem(value)
            ],
          );
        },
        selector: (BuildContext , SubitOrderVM vm) {
          return vm.addressBean;
        },
      ),
    );
  }
}

class AddressItem extends StatelessWidget{

  MemberReceiveAddress? value;

  AddressItem(this.value);

  @override
  Widget build(BuildContext context) {
    if(value==null){
     return Container(
        height: 90.sp,
        alignment: Alignment.center,
        child: Text("您还没有默认地址！快去创建吧"),
      );
    }
     return Column(
       crossAxisAlignment: CrossAxisAlignment.start,
       children: [
         Row(
           children: [
             Container(
               alignment: Alignment.center,
               margin: EdgeInsets.fromLTRB(0, 0, 10.sp, 0),
               padding: EdgeInsets.fromLTRB(6.sp, 1.sp, 6.sp, 2.sp),
               decoration: ShapeDecoration(
                   color: Color(0xff57D5AD),
                   shape: RoundedRectangleBorder(
                       borderRadius: BorderRadius.circular(2.sp))),
               child: Text(
                 "公司",
                 textAlign: TextAlign.center,
                 style: TextStyle(fontSize: 10.sp, color: Colors.white),
                 strutStyle: StrutStyle(height: 1),
               ),
             ),
             Expanded(
                 child: Text(
                   "${value?.city}",
                   style: TextStyle(fontSize: 12.sp, color: Color(0xff2D3137)),
                   overflow: TextOverflow.ellipsis,
                   softWrap: true,
                 )),
           ],
         ),
         Divider(
           height: 10.sp,
           color: Colors.transparent,
         ),
         Text(
           "${value?.detailAddress}",
           style: TextStyle(fontSize: 18.sp, color: Color(0xff2D3137)),
           overflow: TextOverflow.ellipsis,
           softWrap: true,
           maxLines: 2,
           textAlign: TextAlign.left,
         ),
         Divider(
           height: 12.sp,
           color: Colors.transparent,
         ),
         Text(
           "${value?.name}\u3000${value?.phoneNumber}",
           style: TextStyle(fontSize: 14.sp, color: Color(0xff2D3137)),
           overflow: TextOverflow.ellipsis,
           softWrap: true,
           maxLines: 2,
           textAlign: TextAlign.left,
         )
       ],
     );
  }


}

//店铺 级  item
class StoreItemWidget extends StatelessWidget {
  ScrollController controller;
  ConfirmOrderItems? bean;
  SubitOrderVM subitOrderVM;
  StoreItemWidget(this.controller, this.bean, this.subitOrderVM);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(0.sp, 12.sp, 0.sp, 0),
      padding: EdgeInsets.all(14.sp),
      decoration: ShapeDecoration(
          color: Colors.white,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10.r)))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          //店铺名称
          Row(
            children: [
              Image.asset(
                assetPath("ic_tab_store_def.png"),
                height: 15.sp,
                fit: BoxFit.fitHeight,
              ),
              Padding(padding: EdgeInsets.fromLTRB(4.sp, 0, 0, 0)),
              Text(
                bean?.storeName ?? "",
                style: TextStyle(
                    fontSize: 16.sp,
                    color: Color(0xff2D3137),
                    overflow: TextOverflow.ellipsis),
                maxLines: 1,
                softWrap: true,
              ),
              Padding(padding: EdgeInsets.fromLTRB(4.sp, 0, 0, 0)),
              Image.asset(
                assetPath("ic_my_right_arrow.png"),
                height: 9.sp,
                fit: BoxFit.fitHeight,
              ),
            ],
          ),
          ListView.separated(
            padding: EdgeInsets.fromLTRB(0, 20.sp, 0, 0),
            controller: controller,
            itemCount: bean?.checkProducts?.length ?? 0,
            shrinkWrap: true,
            itemBuilder: (BuildContext context, int index) {
              CheckProducts? checkProducts =
                  bean?.checkProducts?[index];
              return GoodsItemWidget(checkProducts);
            },
            separatorBuilder: (BuildContext context, int index) {
              return Divider(
                height: 20.h,
                color: Color(ConstantColors.mainBackgroundColor),
              );
            },
          ),
          Padding(padding: EdgeInsets.fromLTRB(0, 12.sp, 0, 0)),
          //配送
          Container(
            alignment: Alignment.centerLeft,
            height: 35.sp,
            child: Row(
              children: [
                Text(
                  "配送",
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: Color(0xff2D3137),
                      overflow: TextOverflow.ellipsis),
                  maxLines: 1,
                  softWrap: true,
                ),
                Expanded(
                    child: Text(
                  "￥10",
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: Color(0xff2D3137),
                      overflow: TextOverflow.ellipsis,
                      fontWeight: FontWeight.w600),
                  maxLines: 1,
                  softWrap: true,
                  textAlign: TextAlign.right,
                )),
              ],
            ),
          ),
          //相关服务
          Container(
            alignment: Alignment.centerLeft,
            height: 35.sp,
            child: Row(
              children: [
                Text(
                  "相关服务",
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: Color(0xff2D3137),
                      overflow: TextOverflow.ellipsis),
                  maxLines: 1,
                  softWrap: true,
                ),
                Expanded(
                    child: Container(
                  alignment: Alignment.centerRight,
                  child: IntrinsicWidth(
                    child: Container(
                        padding: EdgeInsets.fromLTRB(5.sp, 1.sp, 5.sp, 1.sp),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10.sp),
                            border: Border.all(
                                color: Color(0xffFFB47D), width: 0.5.sp)),
                        child: Text(
                          "七天无理由退货",
                          strutStyle: StrutStyle(height: 1),
                          style: TextStyle(
                              fontSize: 12.sp,
                              color: Color(0xffFFB47D),
                              overflow: TextOverflow.ellipsis,
                              fontWeight: FontWeight.w600),
                          maxLines: 1,
                          softWrap: true,
                          textAlign: TextAlign.center,
                        )),
                  ),
                )),
              ],
            ),
          ),
          //留言
          Container(
            alignment: Alignment.centerLeft,
            height: 35.sp,
            child: Row(
              children: [
                Text(
                  "留言",
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: Color(0xff2D3137),
                      overflow: TextOverflow.ellipsis),
                  maxLines: 1,
                  softWrap: true,
                ),
                Expanded(
                    child: Container(
                        child: Text(
                  "和商家协商一致后留言,最多100字",
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: Color(0xffCDCDCD),
                      overflow: TextOverflow.ellipsis),
                  maxLines: 3,
                  softWrap: true,
                  textAlign: TextAlign.right,
                ))),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
//商品级 item
class GoodsItemWidget extends StatelessWidget {
  CheckProducts? bean;

  GoodsItemWidget(this.bean);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: [
          DufRoundedWidget(assetImage(bean?.productPic ?? "",
              width: 90.sp, height: 90.sp, fit: BoxFit.fill)),
          Expanded(
            child: Container(
              height: 90.sp,
              alignment: Alignment.centerLeft,
              margin: EdgeInsets.fromLTRB(12.sp, 0, 0, 0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "${bean?.productName ?? "--"}",
                    style: TextStyle(
                        fontSize: 14.sp,
                        color: Color(0xff2D3137),
                        overflow: TextOverflow.ellipsis),
                    maxLines: 1,
                    softWrap: true,
                  ),
                  Container(
                      padding: EdgeInsets.fromLTRB(8.sp, 5.sp, 8.sp, 5.sp),
                      decoration: ShapeDecoration(
                          color: Color(0xffF5F6F8),
                          shape: RoundedRectangleBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(11.r)))),
                      child: Text(
                        "${bean?.productAttr ?? ""}",
                        strutStyle: StrutStyle(height: 1),
                        style: TextStyle(
                            fontSize: 12.sp,
                            color: Color(0xff999999),
                            overflow: TextOverflow.ellipsis),
                        maxLines: 1,
                        softWrap: true,
                      )),
                  Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Expanded(
                          child: PriceInfoDealWith(
                        priceText: '${bean?.askPrice ?? ""}',
                        priceSize: 20.sp,
                        isAskPriceFlag: bean?.isAskPriceFlag,
                      )),
                      Container(
                          alignment: Alignment.center,
                          constraints:
                              BoxConstraints(minWidth: 35.sp, maxWidth: 50.sp),
                          padding: EdgeInsets.all(3.sp),
                          decoration: ShapeDecoration(
                              color: Color(0xffF5F6F8),
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(4.r)))),
                          child: Text(
                            '${bean?.quantity ?? ""}',
                            style: TextStyle(
                                fontSize: 12.sp,
                                color: Color(0xff2D3137),
                                overflow: TextOverflow.ellipsis),
                            maxLines: 1,
                            softWrap: true,
                          )),
                    ],
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}

//全款支付
class PaymentFrom extends StatelessWidget {
  const PaymentFrom({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
       margin: EdgeInsets.fromLTRB(0, 12.sp, 0, 0),
       padding: EdgeInsets.all(14.sp),
       decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(10.sp)),
       child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text("付款条款",style: TextStyle(fontSize: 14.sp,color: Color(0xff2D3137),fontWeight: FontWeight.w600)),
            Padding(padding: EdgeInsets.fromLTRB(0, 10.sp, 0, 0)),
            ItemSelectorWidge(ic: "ic_pay_money_type.png", name: "全额付款", initCheckBoxValue: true),
          ],
       ),
    );
  }
}

//发票
class Invoice extends StatelessWidget {
  const Invoice({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(0, 12.sp, 0, 0),
      padding: EdgeInsets.fromLTRB(14.sp, 20.sp, 14.sp, 20.sp),
      decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(10.sp)),
      child: Row(
         children: [
           Text("发票",style: TextStyle(fontSize: 14.sp,color: Color(0xff2D3137))),
           Expanded(child: Text("开具发票",style: TextStyle(fontSize: 14.sp,height: 1.4,color: Color(0xff2D3137)),textAlign: TextAlign.right,),),
           Padding(padding: EdgeInsets.fromLTRB(0, 0, 8.sp, 0)),
           Image.asset(assetPath("ic_my_right_arrow.png"),height: 9.sp,fit: BoxFit.fitHeight,),
           Padding(padding: EdgeInsets.fromLTRB(0, 0, 4.sp, 0)),
         ],
      ),
    );
  }
}

//付款方式
class Payway extends StatelessWidget {
  const Payway({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(0, 12.sp, 0, 0),
      padding: EdgeInsets.all(14.sp),
      decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.circular(10.sp)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("支付方式",style: TextStyle(fontSize: 14.sp,color: Color(0xff2D3137),fontWeight: FontWeight.w600)),
          Padding(padding: EdgeInsets.fromLTRB(0, 10.sp, 0, 0)),
          ItemSelectorWidge(ic: "ic_pay_alipay.png", name: "支付宝", initCheckBoxValue: true),
          ItemSelectorWidge(ic: "ic_pay_wechat.png", name: "微信", initCheckBoxValue: false),
          ItemSelectorWidge(ic: "ic_order_total_price.png", name: "对公转账", initCheckBoxValue: false),
        ],
      ),
    );
  }
}

//模版
class ItemSelectorWidge extends StatelessWidget {
  
  String ic;
  String name;
  bool initCheckBoxValue;
  ValueChanged<bool>? onChanged;

  ItemSelectorWidge({Key? key,required this.ic,required this.name,required this.initCheckBoxValue, this.onChanged}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 48.sp,
        alignment: Alignment.centerLeft,
        child: Row(
           children: [
             Image.asset(assetPath(ic),width: 24.sp,height: 24.sp,),
             Padding(padding: EdgeInsets.fromLTRB(8.sp, 0, 0, 0),),
             Expanded(child: Text(name,style: TextStyle(fontSize: 14.sp,color: Color(0xff2D3137)),textAlign: TextAlign.left,)),
             DufCheckbox(value: initCheckBoxValue, radius: 9.sp,onChanged: onChanged,),
           ],
        ),
    );
  }
}
